$(function() {
	var service = new EbeimService();
	var user = plugin.cookieToJson(document.cookie);
	var data1, data2;
	var jusername = $("#j-username"), jrole = $("#j-role");
	var date = new Date(),
		today = date.getFullYear()+"-"+date.getMonth()+"-"+date.getDate();

	jusername.html(user.username);
	if (user.role == "SYSADMIN") {
		jrole.html("系统管理员");
	} else if (user.role == "ADMIN") {
		jrole.html("管理员");
	} else {
		jrole.html("普通用户");
	}

	service
			.getFirstPageStatics(
					{
						userId : user.userId
					},
					function(json) {
						if (json.code == 200) {
							data1 = {
								color : "#FFAE00",
								text : "存储空间" + '\n' + fileSizeTransferNoPoint(json.statics.usageSize)
										 + "/" + fileSizeTransferNoPoint(json.statics.countSize),
								value : ((json.statics.usageSize / json.statics.countSize) * 100)
										.toFixed(2),
							}
							var date1 = json.customer.createTimeStr.substring(0,json.customer.createTimeStr.indexOf(" ")),
							    date2 = json.serverInfo.endTime.substring(0,json.serverInfo.endTime.indexOf(" ")),
							    allDays = DateDiff(date1,date2);
							data2 = {
								color : "#0DE6E9",
								text : "服务到期时间" + '\n'
										+ json.serverInfo.endTime.split(" ")[0],
								value : (((allDays - json.serverInfo.days) / allDays) * 100).toFixed(2)
							}
							ringChart.diagram("chart-space", data1);
							ringChart.diagram("chart-time", data2);
						} else {
							plugin.msgtip({
								type : "i",
								msg : json.desc
							});
						}
					});

})

/*
 * 定义一个环形统计图 @domId:初始化统计图dom Id @data{ value:统计数值 }
 */
var ringChart = {
	random : function(l, u) {
		return Math.floor((Math.random() * (u - l + 1)) + l);
	},
	diagram : function(domId, data) {
		var r = Raphael(domId, 134, 134), rad = 25, defaultText = data.text, speed = 250;

		r.circle(67, 67, 50).attr({
			stroke : 'none',
			fill : '#1567af'
		});

		var title = r.text(67, 67, defaultText).attr({
			font : '12px 微软雅黑',
			fill : '#fff'
		}).toFront();

		r.customAttributes.arc = function(value, color, rad) {
			var v = 3.6 * value, alpha = v == 360 ? 359.99 : v, random = ringChart
					.random(90, 90), a = (random - alpha) * Math.PI / 180, b = random
					* Math.PI / 180, sx = 67 + rad * Math.cos(b), sy = 67 - rad
					* Math.sin(b), x = 67 + rad * Math.cos(a), y = 67 - rad
					* Math.sin(a), path = [ [ 'M', sx, sy ],
					[ 'A', rad, rad, 0, +(alpha > 180), 1, x, y ] ];
			return {
				path : path,
				stroke : color
			}
		}

		var color = data.color, value = data.value, text = data.text;

		rad += 30;
		r.path().attr({
			arc : [ 100, "#50A2EE", rad ],
			'stroke-width' : 10
		});
		/*
		 * var aaaa = 0; var z,c; var c = r.path().attr({ arc: [aaaa, color,
		 * rad], 'stroke-width': 10 }); var dddd = setInterval(function(){
		 * c.remove(); z = r.path().attr({ arc: [aaaa, color, rad],
		 * 'stroke-width': 10 }); c = z; if(aaaa < value){ aaaa++; }else{
		 * clearInterval(dddd); } },5)
		 */

		var z = r.path().attr({
			arc : [ value, color, rad ],
			'stroke-width' : 10
		});
		z.hover(function() {
			this.animate({
				'stroke-width' : 20,
				opacity : .75
			}, 1000, 'elastic');
			title.stop().animate({
				opacity : 0
			}, speed, '>', function() {
				this.attr({
					text : text + '\n' + value + '%'
				}).animate({
					opacity : 1
				}, speed, '<');
			});
		}, function() {
			this.stop().animate({
				'stroke-width' : 10,
				opacity : 1
			}, speed * 4, 'elastic');
			title.stop().animate({
				opacity : 0
			}, speed, '>', function() {
				title.attr({
					text : defaultText
				}).animate({
					opacity : 1
				}, speed, '<');
			});
		});

	}
}

//计算天数差的函数，通用  
function  DateDiff(sDate1,  sDate2){    //sDate1和sDate2是2006-12-18格式  
    var  aDate,  oDate1,  oDate2,  iDays  
    aDate  =  sDate1.split("-")  
    oDate1  =  new  Date(aDate[1]  +  '-'  +  aDate[2]  +  '-'  +  aDate[0])    //转换为12-18-2006格式  
    aDate  =  sDate2.split("-")  
    oDate2  =  new  Date(aDate[1]  +  '-'  +  aDate[2]  +  '-'  +  aDate[0])  
    iDays  =  parseInt(Math.abs(oDate1  -  oDate2)  /  1000  /  60  /  60  /24)    //把相差的毫秒数转换为天数  
    return  iDays  
}  
